<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Blog_m extends CI_Model {

    /**
     * Searches blog posts based on supplied data array
     * @param $data array
     * @return array
     */
    public function search($data = array())
    {
        if (array_key_exists('category_id', $data))
        {
                $this->db->where('category_id', $data['category_id']);
        }

        if (array_key_exists('status', $data))
        {
                $this->db->where('status', $data['status']);
        }

        if (array_key_exists('keywords', $data))
        {
                $matches = array();
                if (strstr($data['keywords'], '%'))
                {
                        preg_match_all('/%.*?%/i', $data['keywords'], $matches);
                }

                if (!empty($matches[0]))
                {
                        foreach ($matches[0] as $match)
                        {
                                $phrases[] = str_replace('%', '', $match);
                        }
                }
                else
                {
                        $temp_phrases = explode(' ', $data['keywords']);
                        foreach ($temp_phrases as $phrase)
                        {
                                $phrases[] = str_replace('%', '', $phrase);
                        }
                }

                $counter = 0;
                foreach ($phrases as $phrase)
                {
                        if ($counter == 0)
                        {
                                $this->db->like('blog.title', $phrase);
                        }
                        else
                        {
                                $this->db->or_like('blog.title', $phrase);
                        }

                        $this->db->or_like('blog.body', $phrase);
                        $this->db->or_like('blog.intro', $phrase);
                        $this->db->or_like('profiles.display_name', $phrase);
                        $counter++;
                }
        }
        return $this->get_all();
    }
    
    /**
     * Get object detail by $id
     * @param int $id
     * @return array 
     */
    public function get_object_detail($id)
    {
        $result =  $this->db->select('*')->join('object_extend', 'object_id = object.id', 'left')
                                  ->where(array('object.id' => $id))
				  ->get('object')
				  ->row_array();
        
        if($result){
            $query = "UPDATE iemvn_object SET num_views = num_views + 1 WHERE id = " . $id;
            
            $this->db->query($query);
        }
        
        return $result;
    }
    
    /**
     * Get top object by categoryId
     * @param int $cateId
     * @param int $limit
     * @param int @offset 
     */
    public function get_top_object_by_cateId($cateId, $limit = 3, $offset = 0)
    {
        //Set query
        $query = "SELECT SQL_CALC_FOUND_ROWS * FROM iemvn_object ";
        $query.= "WHERE category_id = " . $cateId . " ";
        $query.= "AND status = 1 ";
        $query.= "ORDER BY created_on DESC LIMIT " . $offset . ", " . $limit;
        
        //Get data
        $response['data']  = $this->db->query($query)
                                      ->result_array();
        
        //Set query total
        $query = 'SELECT FOUND_ROWS() AS total';
        
        //Get total
        $response['total'] = $this->db->query($query)->row('total');
        
        //Return data
        return $response;
    }
    
    /**
     * Get top object by parentId
     * @param int $parentId
     * @param int $limit
     * @param int @offset
     * @param array $params 
     */
    public function get_top_object_by_parentId($parentId, $limit = 3, $offset = 0, $params = array(), $order = 'created_on')
    {
        //Set query
        $query = "SELECT SQL_CALC_FOUND_ROWS Obj.* ";
        $query.= "FROM iemvn_object AS Obj, iemvn_categories AS Cate ";
        $query.= "WHERE (Cate.id = " . $parentId . " OR Cate.parent_id = " . $parentId . ") ";
        $query.= "AND Obj.category_id = Cate.id " ;
        $query.= "AND Obj.status = 1 " ;
        if(!empty($params)){
            foreach ($params as $key => $value){
                $query.= "AND " . $key . " = " . $value . " ";
            }
        }
        $query.= "ORDER BY Obj.". $order ." DESC LIMIT " . $offset . ", " . $limit;
        
        //Get data
        $response['data'] = $this->db->query($query)->result_array();
        
        //Set query total
        $query = 'SELECT FOUND_ROWS() AS total';
        
        //Get total
        $response['total'] = $this->db->query($query)->row('total');
        
        //Return data
        return $response;
    }

}